package cn.easier.activity.console.repository;

import cn.easier.activity.console.entity.BargainGoodsOrder;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 订单信息 Repository
 */
@Repository
public interface BargainGoodsOrderRepository extends JpaRepository<BargainGoodsOrder, Integer>, JpaSpecificationExecutor<BargainGoodsOrder> {
    @Query(value = "select a.* " +
            "from bargain_goods_order a " +
            "where ((:activityId is null or :activityId = '') or a.`activity_id` = :activityId) " +
            "and (:phoneNum is null or a.defined_order_uer = :phoneNum) " +
            "and (:bargainOrderNo is null or a.bargain_order_no = :bargainOrderNo) " +
            "order by a.create_time desc LIMIT :pageCount , :pageSize ", nativeQuery = true)
    List<BargainGoodsOrder> findAllBy(@Param("activityId") Integer activityId,
                                      @Param("phoneNum") String phoneNum,
                                      @Param("bargainOrderNo") String bargainOrderNo,
                                      @Param("pageCount") int pageCount,
                                      @Param("pageSize") int pageSize);

    @Query(value = "select count(a.bargain_order_no) " +
            "from bargain_goods_order a " +
            "where ((:activityId is null or :activityId = '') or a.`activity_id` = :activityId) " +
            "and (:phoneNum is null or a.defined_order_uer = :phoneNum) " +
            "and (:bargainOrderNo is null or a.bargain_order_no = :bargainOrderNo) " +
            "order by a.bargain_order_no desc ", nativeQuery = true)
    long findAllByCount(@Param("activityId") Integer activityId,
                        @Param("phoneNum") String phoneNum,
                        @Param("bargainOrderNo") String bargainOrderNo);

}
